<!DOCTYPE html>
<html>
<head>
<title>新增治疗单据项目信息</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css" media="all">
<style type="text/css">
select {
	height: 38px;
	line-height: 1.3;
	line-height: 38px\9;
	border-width: 1px;
	border-style: solid;
	background-color: #fff;
	border-radius: 2px;
	width: 100%;
}

h1 {
	color: #c2c2c2;
	line-height: 25px;
	font-size: 16px;
	text-align: center;
	margin-top: 5px;
}

legend {
	font: 22px Helvetica Neue, Helvetica, PingFang SC, 微软雅黑, Tahoma, Arial,
		sans-serif;
	width: auto;
	margin: 0px auto;
}

.row {
	margin-top: 10px;
}

.no_border {
	border: 0px;
}

#btn_search {
	margin-top: 8px;
	margin-left: -25px;
}
</style>
</head>

<body>
	<div id="div_treatBill" class="layui-form layui-container" style="">
		<fieldset style="border-width: 0px;margin-top:25px">
			<legend>
				<a>治疗单据项目信息</a>
			</legend>
		</fieldset>
		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">治疗项目: </span>
			</div>
			<div class="layui-col-xs5 layui-col-sm3 layui-col-md2 ">
				<select id="projectNameSelect" name="projectName" lay-verify="required" lay-serach="" lay-filter="projectNameSelect">
                </select>
			</div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">治疗次数: </span>
			</div>
			<div class="layui-col-xs5 layui-col-sm3 layui-col-md2">
				<input type="text" name="number" class="layui-input" lay-verify="number">
            </div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">费用金额: </span>
			</div>
			<div class="layui-col-xs5 layui-col-sm3 layui-col-md2">
				<input type="text" name="cost" lay-verify="number" placeholder="￥" autocomplete="off" class="layui-input"
                disabled="disabled">
           </div>
		</div>

		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">截至日期: </span>
			</div>
			<div class="layui-col-xs5 layui-col-sm3 layui-col-md2">
				<input id="deadline" name="deadline" type="text" class="layui-input" readonly>
            </div>
		</div>
		
		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">有效期限: </span>
			</div>
			<div class="layui-col-xs4 layui-col-md2 ">
				<input id="dayNumber" name="dayNumber" class="layui-input" type="text">
			</div>
			<div class="layui-col-xs1 layui-col-md1 ">
				<h1>(天)</h1>
			</div>
		</div>
		
		<div class="layui-row row">
			<div class="layui-col-xs3 layui-col-xs-offset1 layui-col-sm2 layui-col-sm-offset1 layui-col-md2 layui-col-md-offset2">
				<span class="layui-form-label">其他备注: </span>
			</div>
			<div class="layui-col-xs7 layui-col-sm4 layui-col-md3">
				<textarea name="extraInfo" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
		</div>
		<input id="submit" style="display:none" type="button" lay-submit lay-filter="*">
	</div>
</body>

<script type="text/javascript" src="/Hospital/lib/jquery.min.js"></script>
<script type="text/javascript" src="/Hospital/lib/layui-v2.1.6/layui/layui.js"></script>
<script>
    var isSuccess=0;        //判断是否新增成功
    layui.use(['form','laydate'],function () {
        var form=layui.form;
        var $=layui.jquery;
        var laydate = layui.laydate; //调用Layui插件里的时间组件
        form.render();   //layui form表单更新渲染
        
        listen();        //监听事件
        initFrame();     //初始化窗口数据
        //resize();        //屏幕大小自适应
        //editData();      //编辑按钮打开此页面时获取数据并初始化
        
       	function initFrame() {  //初始化窗口
        	initDeadline(); //初始化截至日期
        	var operator = localStorage.getItem("importTreatBill_operator");
        	if(operator == 1){	//添加
        		initAddFrame();
        	}
        	
        	if(operator == 2) {		//编辑
        		initAlterFrame();
        	}
        	
        }
      	
        function initAddFrame() {
        	$.ajax({
        		type : "post",
        		url : "/Hospital/projectController/getAllTreatmentProject",
        		data : "",
        		success : function(response) {
        			if(response.code != 200) {
        				console.error(response);
        				return;
        			}
        			var result = response.result;
					for (var i = 0; i < result.length; i++) {
						var projectId = result[i].projectId;
						var projectName = result[i].projectName;
						var unitPrice = result[i].unitPrice;
						var name = result[i].name;
						$("#projectNameSelect").append(
								"<option value="+projectId+" unitPrice="+unitPrice+">" + projectName + "</option>");
					}
					//重绘
					form.render();
        		}
        	});
        }
        
        function initAlterFrame() {
        	//编辑对象的信息
        	var editData = JSON.parse(localStorage.getItem("editTableData"));
        	console.log("addTreatBillProject.html/initAlterFrame.editData");
        	console.log(editData);
        	$.ajax({
        		type : "post",
        		url : "/Hospital/projectController/getAllTreatmentProject",
        		data : "",
        		success : function(response) {
        			if(response.code != 200) {
        				console.error(response);
        				return;
        			}
        			var result = response.result;
					for (var i = 0; i < result.length; i++) {
						var projectId = result[i].projectId;
						var projectName = result[i].projectName;
						var unitPrice = result[i].unitPrice;
						var name = result[i].name;
						$("#projectNameSelect").append(
								"<option value="+projectId+" unitPrice="+unitPrice+">" + projectName + "</option>");
					}
					$("#projectNameSelect").val(editData.projectId);
	                $("input[name='number']").val(editData.number);
	                $("input[name='cost']").val(editData.cost);
	                $("textarea[name='extraInfo']").val(editData.extraInfo);
	                $("#deadline").val(editData.deadline);
					//重绘
					form.render();
        		}
        	});
        }
        
        function initDeadline() {
        	var nowdate = new Date(); //获取当前时间
			var nowYear = nowdate.getFullYear() + "-";//获取当前年份
			nowYear += (nowdate.getMonth() + 1) + "-";
			nowYear += nowdate.getDate() + 1;
			//console.log("addTreatBillProject.html/initDeadline.nowYear");
			//console.log(nowYear);
			laydate.render({ //执行一个laydate实例
				elem : "#deadline", //指定元素
				theme : "#55565b",
				value : nowYear,
				min : nowYear,
			});
        }
        
        function editData(){    //编辑按钮打开此页面时获取数据并初始化
            var editData=JSON.parse(localStorage.getItem("editTableData"));
            localStorage.removeItem("editTableData");
            if(editData!=null){
                $("#projectNameSelect option").eq(editData.projectId).attr("selected",true);
                $("input[name='number']").val(editData.number);
                $("input[name='cost']").val(editData.cost);
                $("textarea[name='extraInfo']").val(editData.extraInfo);
                form.render();
            }
        }
        
        function resize(){
            var bodyHeight=parent.$('#iframeHeight').val()*0.7;
            var bodyWidth=parent.$('#iframeWidth').val()*0.96;
            $('body').css({'height': '300px','width':bodyWidth}); //写死了body高度
            /* var iconSize=$(document.body).width()*0.05;
            $('.icon').css({'font-size':iconSize}); */
        }
        
        function listen(){
            form.on('submit(*)',function () {   //提交监听
                var data=formData();     //获取表单数据
                localStorage.setItem("importBillIframeData",JSON.stringify(data));  //传值
                isSuccess=200;
                $("select[name='projectName']").find("option:selected").val("");
                $("input[name='number']").val("");
                $("input[name='cost']").val("");
                $("textarea[name='extraInfo']").val("");
                form.render();
            });
        	
          
            $('input[name="number"]').change(function () {    //输入次数监听
            	//当治疗次数发生改变时,费用金额为次数乘以价格
            	var numberValue = $('input[name="number"]').val(); //次数的值
            	numberValue = vertifyNumber(numberValue); //如果有非法字符返回1;如果不存在取整数舍弃小数
            	$('input[name="number"]').val(numberValue); 
            		
                //获取治疗项目的单次价格，然后乘次数
                var unitPrice = $("#projectNameSelect option:selected").attr("unitPrice");//获取治疗项目的单次价格
                //设置费用
                $('input[name="cost"]').val(unitPrice * numberValue);
            });
            
            form.on('select(projectNameSelect)', function(data){ //治疗项目下拉栏监听事件
            	var numberValue = $('input[name="number"]').val(); //次数的值
            	numberValue = vertifyNumber(numberValue); //如果有非法字符返回1;如果不存在取整数舍弃小数
            	$('input[name="number"]').val(numberValue); 
            	
                //获取治疗项目的单次价格，然后乘次数
                var unitPrice = $("#projectNameSelect option:selected").attr("unitPrice");//获取治疗项目的单次价格
                //设置费用
                $('input[name="cost"]').val(unitPrice * numberValue);
			}); 		
        }
    });
    function formData(){        //获取表单数据
        var projectId=$("select[name='projectName']").find("option:selected").val();
        var projectName=$("select[name='projectName']").find("option:selected").text();
        var number=$("input[name='number']").val();
        var cost=$("input[name='cost']").val();
        var extraInfo=$("textarea[name='extraInfo']").val();
        var deadline = $("#deadline").val();
        var data={
            projectId:projectId,
            projectName:projectName,
            number:number,
            cost:cost,
            deadline: deadline,
            extraInfo:extraInfo
        };
        return data;
    }
    function importCureSubmit(){      //父框点击新增框的新增按钮调用此函数
        $('#submit').click();
        var is=isSuccess;
        isSuccess=0;
        return is;
    }
    
    function vertifyNumber(x) {  //验证次数输入值合法性
    	var f1 = parseFloat(x); //判断"a11"类的错误
		if (isNaN(f1)) {
			return 1;
		}
		var f2 = Math.round(x); //判断"11a"类的错误 
		if (isNaN(f2)) {
			return 1;
		}
    	return f2;
    }
</script>
</html>
